<template>
  <div class="app-container">
    <el-card>
      <el-row :gutter="20" align="middle">
        <el-col :span="24" class="input-button-group">
          <el-select v-model="communityId" placeholder="选择小区名称" clearable style="width: 350px;">
            <el-option
              v-for="community in communityList"
              :key="community.id"
              :label="community.name"
              :value="community.id"
            />
          </el-select>
          <el-input v-model="number" placeholder="输入停车位编号" clearable style="width: 200px;" />
          <el-select v-model="statusId" placeholder="选择状态" clearable style="width: 400px;">
            <el-option :value="2" label="闲置" />
            <el-option :value="3" label="出租" />
            <el-option :value="4" label="已售" />
          </el-select>
          <el-button icon="el-icon-search" @click="handleSearch">筛选</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      height="532"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="100" sortable :sort-by="sortById">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>

      <el-table-column label="小区名称" width="170" align="center">
        <template slot-scope="scope">
          {{ scope.row.communityName }}
        </template>
      </el-table-column>

      <el-table-column label="停车位编号" width="170" align="center">
        <template slot-scope="scope">
          {{ scope.row.number }}
        </template>
      </el-table-column>

      <el-table-column label="状态" width="170" align="center">
        <template slot-scope="scope">
          <template v-if="isEditing(scope.row.id)">
            <el-select v-model="scope.row.statusId" clearable size="small">
              <el-option :value="2" label="闲置" />
              <el-option :value="3" label="出租" />
              <el-option :value="4" label="已售" />
            </el-select>
          </template>
          <template v-else>
            <span v-if="scope.row.statusId === 2" style="color: #999;">闲置</span>
            <span v-if="scope.row.statusId === 3" style="color: #4CAF50;">出租</span>
            <span v-if="scope.row.statusId === 4" style="color: #FF9800;">已售</span>
          </template>
        </template>
      </el-table-column>

      <el-table-column label="所属业主" width="170" align="center">
        <template slot-scope="scope">
          <template v-if="isEditing(scope.row.id)">
            <el-input v-model="scope.row.ownerName" size="small" />
          </template>
          <template v-else>
            {{ scope.row.ownerName }}
          </template>
        </template>
      </el-table-column>

      <el-table-column label="更新时间" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.updateTime }}
        </template>
      </el-table-column>

      <!-- Actions Column -->
      <el-table-column label="操作" width="257" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="toggleEdit(scope.row.id)"
          >{{ isEditing(scope.row.id) ? '保存' : '编辑' }}</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页 -->
    <div class="block" style="text-align: center;">
      <el-pagination
        :current-page="page"
        :page-sizes="[9, 20, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getList, updateRow, deleteRow } from '@/api/parking'
import { getCommunityList } from '@/api/community'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      page: 1,
      pageSize: 9,
      communityId: '',
      number: '',
      statusId: '',
      total: 0,
      editingRowId: null,
      communityList: []
    }
  },
  created() {
    this.fetchData()
  },
  mounted() {
    this.fetchCommunityList()
  },
  methods: {
    fetchCommunityList() {
      getCommunityList().then(response => {
        this.communityList = response.data
        console.log(response.data)
      })
    },
    fetchData() {
      this.listLoading = true
      getList({ page: this.page, pageSize: this.pageSize, communityId: this.communityId, number: this.number, statusId: this.statusId })
        .then(response => {
          this.list = response.data.records
          this.total = response.data.total
          this.listLoading = false
        })
    },
    sortById(a, b) {
      return a.id - b.id
    },
    handleSearch() {
      this.page = 1
      this.fetchData()
    },
    handleSizeChange(size) {
      this.pageSize = size
      this.fetchData()
    },
    handleCurrentChange(page) {
      this.page = page
      this.fetchData()
    },
    toggleEdit(rowId) {
      if (this.editingRowId === rowId) {
        this.saveChanges(rowId)
        this.editingRowId = null
      } else {
        this.editingRowId = rowId
      }
    },
    isEditing(rowId) {
      return this.editingRowId === rowId
    },
    saveChanges(rowId) {
      const updatedRow = this.list.find(row => row.id === rowId)
      updateRow(updatedRow).then(() => {
        this.$message.success('修改成功')
        this.fetchData()
      })
    },
    handleDelete(rowId) {
      const ids = [rowId]
      this.$confirm('确定删除该记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRow(ids).then(() => {
          this.$message.success('删除成功')
          this.fetchData()
        })
      }).catch(() => {
        this.$message('已取消删除')
      })
    }
  }
}
</script>

<style scoped>
.input-button-group {
  display: flex;
  align-items: center;
}

.input-button-group .el-input {
  flex: 1;
  margin-right: 10px;
}
</style>
